package news.servlet;

import com.alibaba.fastjson.JSONObject;
import news.mdel.New;
import news.service.NewService;
import news.service.NewServiceImpl;
import util.PageModel;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet(value = "*.new")
public class NewServlet extends HttpServlet {

    NewService newService = new NewServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String path = req.getServletPath();
        switch (path){
            case "/selectByPage.new":
                selectByPage(req,resp);
                break;
            case "/toList.new":
                toList(req,resp);
                break;
            case "/toEdit.new":
                toEdit(req,resp);
                break;
            case "/saveOne.new":
                saveOne(req,resp);
                break;
            case "/selectById.new":
                selectById(req,resp);
                break;
            case "/deleteById.new":
                System.out.println("即将删除");
                deleteById(req, resp);
            default:
                break;
        }
    }

    protected void selectById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String newid = req.getParameter("newid");
        New new1 = newService.selectById(Integer.valueOf(newid));
        String jsonStr = JSONObject.toJSONString(new1);
        resp.getWriter().print(jsonStr);
    }

    protected void saveOne(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        Object o = req.getSession().getAttribute("newToken");
        if (o != null){
            req.getSession().removeAttribute("categoryToken");
            String newid = req.getParameter("newid");
            if (newid != null && !newid.equals("")) {
//                String title = req.getParameter("title");
//                String category = req.getParameter("category");
//                String content = req.getParameter("content");
//                New new1 = new New(Integer.valueOf(newid),title,category,null,content);
//                newService.updateOne(new1);
            } else {
                String title = req.getParameter("title");
                String category = req.getParameter("category");
                String content = req.getParameter("content");
                New new1 = new New(null,title,category,null,content);
                newService.saveOne(new1);
            }
            resp.sendRedirect(req.getContextPath()+"/toList.new?pageNo=1&likeValue=");
        } else {
            resp.sendRedirect(req.getContextPath() + "/token.html");
        }


    }

    protected void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getSession().setAttribute("newToken","newTokenValue");
        String newid = req.getParameter("newid");
        resp.sendRedirect(req.getContextPath() + "/news/news_toadd.html?newid=" + newid);
    }

    protected void deleteById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String newid = req.getParameter("newid");
        newService.deleteOne(Integer.valueOf(newid));
        resp.sendRedirect(req.getContextPath()+"/toList.new?pageNo=1&likeValue=");
    }

    protected void toList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        req.getRequestDispatcher("/news/news_selectByPage.html").forward(req,resp);
    }


    protected void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        String pageNo = req.getParameter("pageNo")==null?"1":req.getParameter("pageNo");
        String likeValue = req.getParameter("likeValue")==null?"":req.getParameter("likeValue");
        PageModel pageModel = new PageModel();
        pageModel.setPageNo(Integer.valueOf(pageNo));
        pageModel.setLikeValue(likeValue);
        List<New> news = newService.selectByPage(pageModel);
        pageModel.setRecordList(news);
        String jsonStr = JSONObject.toJSONString(pageModel);
        resp.getWriter().print(jsonStr);
    }
}
